import { actpageGetDetail, getProductDetail } from './api'

const actionHandler = {
    data() {
        return {
            loading: false
        }
    },
    methods: {
        openPage: function(data, configData) {
            if(window.android) {
                if(data) {
                    if(data.actionType == "selfDefPage") {
                        var location = window.location;
                        window.location.href = location.protocol+"//"+location.host+location.pathname+"?id="+data.selfDefPage;
                    } else if(data.actionType == "selfDefLink") {
                        if(/\.html/.test(data.selfDefLink)) {
                            window.location.href = data.selfDefLink;
                        } else if(/\.(mp4|mov)/.test(data.selfDefLink)) {
                            if(configData&&(configData.componentName == "horizon-video-goodsitem"||configData.componentName == "vert-video-goodsitem")) {
                                if(this.$refs["selfVideo"][0].paused) {
                                    this.$refs["selfVideo"][0].play();
                                } else {
                                    this.$refs["selfVideo"][0].pause();
                                }
                            } else {
                                if(window.android&&window.android.showAdDialog) {
                                    window.android.showAdDialog(data.selfDefLink);
                                }
                            }
                        } else if(/\.(png|jpg|jpeg)/.test(data.selfDefLink)) {
                            if(window.android&&window.android.showAdDialog) {
                                window.android.showAdDialog(data.selfDefLink);
                            }
                        } else if(data.selfDefLink) {
                            window.location.href = data.selfDefLink;
                        }
                    } else if(data.actionType == "associateGoods") {
                        if(window.android&&window.android.showGoodsDetail) {
                            android.showGoodsDetail(data.productId);
                        }
                    } else if(data.actionType == "ignore") {
                        return;
                    }
                }
            } else {
                this.h5WebHandler(data, configData)
            }
        },
        h5WebHandler(data, configData) {
            if(data.actionType === 'selfDefPage') {
                window.location.href = data.selfDefPage;
            } else if(data.actionType === "selfDefLink") {
                window.location.href = data.selfDefLink
            } else if(data.actionType === "associateGoods") {
                this.getProductDetail(data, configData);
            } else if(data.actionType === "ignore") {
                return;
            }
        },
        getProductDetail(data, configData){
            if(this.loading) {
                return;
            }
            
            this.loading = true;
            getProductDetail({id: data.productId})
            .then((data)=> {
                this.loading = false;
                this.$emit("goodsItemDetail", data);
            },(error)=> {
                this.loading = false;
                this.$toast("获取商品详情失败");
            })
        },
        goBackPage: function() {
            if(window.android&&window.android.onBackClick) {
                window.location.href = location.protocol+"//"+location.host+location.pathname+"?id="+window.android.onBackClick();
            } else {
                window.history.go(-1);
            }
        },
        isBackClick: function() {
            if(window.android&&window.android.isBackClick) {
                return window.android.isBackClick();
            } else {
                return false;
            }
        },
        closePage: function() {
            if(window.android&&window.android.finish){
                window.android.finish();
            }
        },
        closeLoading: function() {
            if(window.android&&window.android.finishActivity){
                window.android.finishActivity();
            }
        },
        getH5DiyJsonById: function(pageId) {
            if(window.android) {
                return new Promise(function(resolve, reject) {
                    var pageJson;
                    try {
                       pageJson =  JSON.parse(window.android.getH5DiyJsonById(pageId));
                    } catch(error) {
                        console.log('getPageJson failed');
                        reject();
                        return;
                    }
                    resolve(pageJson);
                })
            } else {
                return new Promise((resolve, reject) => {
                    actpageGetDetail({id: pageId}).then((data) => {
                        resolve(data);
                    })
                })
            }
        }
    },
    closeLoading: function() {
        if(window.android&&window.android.loadComplete){
            window.android.loadComplete();
        }
    }
}

export default actionHandler;